{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "caf2600d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bb4e79a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = pd.read_csv('digit-recognizer/train.csv')\n",
    "train_features = train_data.drop('label',axis=1)/255.0\n",
    "# train_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "12eb9cb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data = pd.read_csv('digit-recognizer/test.csv')\n",
    "test_features = test_data/255.0\n",
    "# test_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7b07b65f",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = np.array(train_features.values,dtype=np.float)\n",
    "test_dataset = np.array(test_features.values,dtype=np.float)\n",
    "train_labels = np.array(train_data.label.values,dtype=np.float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a0510f64",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_dataset = train_dataset.reshape(train_dataset.shape[0], 28, 28, 1)\n",
    "test_dataset = test_dataset.reshape(test_dataset.shape[0], 28, 28, 1)\n",
    "train_dataset = np.pad(train_dataset, ((0,0),(2,2),(2,2),(0,0)), 'constant')\n",
    "test_dataset = np.pad(test_dataset, ((0,0),(2,2),(2,2),(0,0)), 'constant')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = keras.models.Sequential()\n",
    "#Layer 1\n",
    "#Conv Layer 1\n",
    "model.add(keras.layers.Conv2D(filters = 6, \n",
    "                 kernel_size = 5, \n",
    "                 strides = 1, \n",
    "                 activation = 'relu', \n",
    "                 input_shape = (32,32,1)))\n",
    "#Pooling layer 1\n",
    "model.add(keras.layers.MaxPooling2D(pool_size = 2, strides = 2))\n",
    "#Layer 2\n",
    "#Conv Layer 2\n",
    "model.add(keras.layers.Conv2D(filters = 16, \n",
    "                 kernel_size = 5,\n",
    "                 strides = 1,\n",
    "                 activation = 'relu',\n",
    "                 input_shape = (14,14,6)))\n",
    "#Pooling Layer 2\n",
    "model.add(keras.layers.MaxPooling2D(pool_size = 2, strides = 2))\n",
    "#Flatten\n",
    "model.add(keras.layers.Flatten())\n",
    "#Layer 3\n",
    "#Fully connected layer 1\n",
    "model.add(keras.layers.Dense(units = 120, activation = 'relu'))\n",
    "#Layer 4\n",
    "#Fully connected layer 2\n",
    "model.add(keras.layers.Dense(units = 84, activation = 'relu'))\n",
    "#Layer 5\n",
    "#Output Layer\n",
    "model.add(keras.layers.Dense(units = 10, activation = 'softmax'))\n",
    "model.compile(optimizer=tf.keras.optimizers.Adam(0.01),\n",
    "              loss = 'sparse_categorical_crossentropy',\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "aee42730",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 29399 samples, validate on 12601 samples\n",
      "Epoch 1/10\n",
      "29399/29399 - 12s - loss: 0.2032 - accuracy: 0.9368 - val_loss: 0.1354 - val_accuracy: 0.9630\n",
      "Epoch 2/10\n",
      "29399/29399 - 11s - loss: 0.0960 - accuracy: 0.9715 - val_loss: 0.1149 - val_accuracy: 0.9672\n",
      "Epoch 3/10\n",
      "29399/29399 - 11s - loss: 0.0753 - accuracy: 0.9773 - val_loss: 0.0985 - val_accuracy: 0.9730\n",
      "Epoch 4/10\n",
      "29399/29399 - 11s - loss: 0.0748 - accuracy: 0.9795 - val_loss: 0.0826 - val_accuracy: 0.9764\n",
      "Epoch 5/10\n",
      "29399/29399 - 11s - loss: 0.0704 - accuracy: 0.9813 - val_loss: 0.0891 - val_accuracy: 0.9769\n",
      "Epoch 6/10\n",
      "29399/29399 - 11s - loss: 0.0607 - accuracy: 0.9845 - val_loss: 0.0969 - val_accuracy: 0.9790\n",
      "Epoch 7/10\n",
      "29399/29399 - 11s - loss: 0.0722 - accuracy: 0.9819 - val_loss: 0.1449 - val_accuracy: 0.9721\n",
      "Epoch 8/10\n",
      "29399/29399 - 11s - loss: 0.0600 - accuracy: 0.9833 - val_loss: 0.0785 - val_accuracy: 0.9825\n",
      "Epoch 9/10\n",
      "29399/29399 - 11s - loss: 0.0577 - accuracy: 0.9856 - val_loss: 0.1000 - val_accuracy: 0.9806\n",
      "Epoch 10/10\n",
      "29399/29399 - 11s - loss: 0.0680 - accuracy: 0.9833 - val_loss: 0.1185 - val_accuracy: 0.9777\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x2a41dc62048>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#训练\n",
    "model.fit(train_dataset, train_labels, epochs=10, batch_size=64, validation_split=0.3,verbose=2,shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "preds=model.predict(test_dataset)#使用模型对测试集进行预测\n",
    "preds=np.argmax(preds, axis=1)#返回可能性最大的值的索引\n",
    "preds[900]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6624616c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x1d9974f6630>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAOUklEQVR4nO3df4xV9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EfHc/cXJR05afEiSauz+6s1+o+l7XJ66wjuPuTuO7L7H0o6Mc14rc9doq+2qCPsF0v665jH+9RZ8727pN+b2XYz66u7mXHMODHNVnY7veZ+TlY4jXc7nTTNeMc8d81Mf15WHWEfb2qaThr/u97dvyXpnyT9KDtdRWNWSpqt0TkAhyT9os5msmnGn5F0j7uP1NnLWOP01ZbnrY6w75PUPebxTEkHauhjXO5+ILsdlvScRl92dJKDJ2bQzW6Ha+7nC+5+0N0/c/fPJf1SNT532TTjz0ha4+7PZotrf+7G66tdz1sdYd8q6TIz+7qZTZK0RNLzNfTxJWY2OXvjRGY2WdICdd5U1M9LWpbdXybpdzX28jc6ZRrvvGnGVfNzV/v05+7e9h9Jt2j0Hfl3JP1rHT3k9DVL0uvZz1t19yZprUZP6z7R6BnRDySdL2mjpLez26kd1Nt/a3Rq7zc0Gqyumnr7tkZfGr4h6bXs55a6n7tEX2153vi4LBAEn6ADgiDsQBCEHQiCsANBEHYgCMIOBEHYgSD+H+q3p6G9MB/pAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "sample = np.array(train_features.iloc[900, :])\n",
    "sample = sample.reshape([28,28])\n",
    "plt.imshow(sample, cmap='gray')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3b0554df",
   "metadata": {},
   "outputs": [],
   "source": [
    "#preds=np.array(model.predict_classes(test_dataset),dtype=np.int32)\n",
    "preds=model.predict(test_dataset)\n",
    "preds=np.argmax(preds, axis=1)\n",
    "submission = pd.DataFrame({\n",
    "    'ImageId': [i for i in range(1,28001)],\n",
    "    'Label':preds\n",
    "})\n",
    "submission.to_csv('submission.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
